﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MauiTurntable.dbModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Input;

namespace MauiTurntable.ViewModel
{
    /// <summary>
    /// 转盘视图
    /// </summary>
    public partial class cvTurnTableModel : ObservableObject
    {
        /// <summary>
        /// 绘制
        /// </summary>
        [ObservableProperty]
        private GraphicsDrawable _graphics;
        /// <summary>
        /// 项目组
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<string> _groip;

        /// <summary>
        /// 选择的项目
        /// </summary>
        private string selectGroup;
        /// <summary>
        /// 选择的项目
        /// </summary>
        public string SelectGroup
        {
            get => selectGroup;
            set
            {
                SetProperty(ref selectGroup, value);
                if (dBItems != null && _graphics != null && !string.IsNullOrEmpty(selectGroup))
                {
                    var data = dBItems.FindAll(x => x.groupName == selectGroup);
                    _graphics.Sub(data);
                    Re?.Invoke();
                }
            }
        }
        [ObservableProperty]
        private float rWidth;
        [ObservableProperty]
        private float rHeight;
        /// <summary>
        /// 选择的项目
        /// </summary>
        [ObservableProperty]
        private string checkItem;
        private bool _taskRun;
        public bool taskRun { get => _taskRun; set { _taskRun = value; OnPropertyChanged(); } }
        public ICommand RunCommand { get; set; }
        DalItem dal { get; set; }

        private List<DBItem> dBItems { get; set; }
        public event Action Re;
        public event Action<int> Rotation;
        /// <summary>
        /// 
        /// </summary>
        public cvTurnTableModel()
        {
            dal = new DalItem();
            _graphics = new GraphicsDrawable();
            _graphics.outdata += _graphics_outdata;
            RunCommand = new RelayCommand(RunCommandMethod);
            _groip = new ObservableCollection<string>();
            Task.Factory.StartNew(async () =>
            {
                taskRun = false;
                dBItems = await dal.SelectAllData();
                var gdata = dBItems.GroupBy(x => x.groupName);
                foreach (var item in gdata)
                {
                    _groip.Add(item.Key);
                }
                if (_groip.Count > 0)
                {
                    selectGroup = _groip[0];
                }
                taskRun = true;
            });
        }

        private void _graphics_outdata(RectF obj)
        {
            rWidth = obj.Width;
            rHeight = obj.Height;
        }

        private void RunCommandMethod()
        {
            Rotation?.Invoke(new Random().Next(360 * 5, 360 * 7));
        }
    }
}
